


### TOOLS FOR CONFIDENCE REGION

# Base type to omit (Z = "000", no projects):

t_base <- which( types[,1] == 1 & rowSums( types[,2:ncol(types)] ) == 0 )

# Also omit any types with zero counts:
# (e.g., in case sample omitted any isolated types)

t_zero <- which(counts == 0)

# Covariance matrix (w/out types omitted above):

V <- -obs_shares[-c(t_base,t_zero)] %o% obs_shares[-c(t_base,t_zero)]

# Replace diagonal with variances:

diag(V) <- obs_shares[-c(t_base,t_zero)] * (1-obs_shares[-c(t_base,t_zero)])

# Cholesky decomposition of inverse variance matrix:

A <- solve(V)

C <- chol(A) * sqrt(N_auth_exp)

# Expand matrix to include base type:
# (and other omitted types, if any)

C_full <- matrix(0, N_types, N_types)

C_full[-c(t_base,t_zero), -c(t_base,t_zero)] <- C

# Include naive variance for base type share:
# (use square root of inverse variance for C)

p_base <- obs_shares[t_base]

C_full[t_base,t_base] <- sqrt( N_auth_exp / p_base / (1-p_base) )

# Half-width of hypercube:

N_types_pos <- sum(obs_shares > 0)

1 - (1 - 0.95^(1/N_types_pos)) / 2

qnorm( (1 - 0.95^(1/N_types_pos)) / 2 )

hyp_lim <- -qnorm( (1 - 0.95^(1/N_types_pos)) / 2 )
